/**
 * 日历管理 列表页
 *
 * 作者 ganjun
 * 日期 2024-11-21 16:56:51
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-calendar'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#calendar-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'calendarTable',
            url: ctx + 'calendar/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'startTime', title: '开始时间', minWidth: 120},
                {field: 'endTime', title: '结束时间', minWidth: 120},
                {field: 'memo', title: '备注', minWidth: 120},
                {title: '操作', toolbar: '#calendar-tool', width: 260,align:'center'}
            ]],
            toolbar: '#calendar-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(calendarTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(calendarTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增', 'calendar/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['40%', '40%'] : ['40%', '40%'],
                yes: function (index, layero) {
                    $('#calendar-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });
        }
    });

    table.on('tool(calendarTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.id);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'calendar/update/' + data.id, {
                area: [$(window).width() <= 750 ? '95%':'50','800px'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#calendar-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }
    });

    // $delete.on('click', function () {
    //     var checkStatus = table.checkStatus('calendarTable');
    //     if (!checkStatus.data.length) {
    //         febs.alert.warn('没有选择要删除的数据！');
    //     } else {
    //         febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
    //             var ids = [];
    //             layui.each(checkStatus.data, function (key, item) {
    //                 ids.push(item.id);
    //             });
    //             deleteIds(ids.join(','));
    //         });
    //     }
    // });

    function getQueryParams() {
        return {
            // name: $searchForm.find('input[name="name"]').val().trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'calendar/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});